#include "touchkey.h"
#include"jpg.h"
#include <pthread.h>
#include <semaphore.h>
FILE *fp;
int fd;
int sound = -20;
int subcount = 0;
int pluscount = 0;
int touchtype = -1;
char *fbmem;
char *Frist;
bool exitflag = false;
struct fb_var_screeninfo vinfo;
void *play(void *arg)
{
	
	int k = *(int *)arg;
	char *command = calloc(1,50);
	
	system("killall -s SIGKILL madplay ");
	printf("播%d\n",k+1);

	snprintf(command,50,"madplay -a %d ./mp3/d%d.mp3",sound,k+1);
	system(command);
	
}
void *touchType(void *arg)
{
	int k;
	bool xDone=false;
	bool yDone=true;
	int rx,ry;
	bool fflag=true;
	pid_t pid = *(pid_t *)arg;
	struct input_event buf;
	bzero(&buf,sizeof(buf));
	while(1)
	{
		read(fd, &buf, sizeof(buf));
		if(buf.type == EV_ABS)
		{
			if(buf.code == ABS_X&& yDone)
			{
				rx = buf.value;
				yDone = false;
				xDone= true;
			
			}
			if(buf.code == ABS_Y && xDone)
			{
				ry = buf.value;
				yDone = true;
				xDone=false;
				fflag=false;
			}
		}
		if ( yDone && !xDone && !fflag)
		{
			if(rx >= 23 && rx <= 120 && 
				ry >= 440 && ry <= 470 )
			{
			   	touchtype = soundPlus;
			   	pluscount++;
			   	fflag = true;
			}
			if(rx >= 173 && rx <= 258 &&
			   ry >= 407 && ry <= 462 )
			{
				touchtype = soundSub;
				subcount++;
				fflag = true;
			}
			if(rx >= 723 && rx <= 786 &&
			   ry >= 410 && ry <= 473)
			{
				char buf[50];
				showFrist(Frist,fbmem,&vinfo,0,0);
				free(Frist);
				snprintf(buf,50,"kill -s SIGKILL %d",pid);
				system("killall -s SIGKILL madplay");
				system(buf);
			}
		}
	}

}
int main(int argc,char **argv)
{

	//fp=fopen("/dev/input/event0","r");
	fd=open("/dev/input/event0",O_RDONLY);
	int lcd = open("/dev/fb0",O_RDWR);
	//struct fb_var_screeninfo vinfo;
	bzero(&vinfo,sizeof(vinfo));
	ioctl(lcd,FBIOGET_VSCREENINFO, &vinfo);
	int screensize=vinfo.xres*vinfo.yres*vinfo.bits_per_pixel/8;
	fbmem=mmap(NULL,screensize,PROT_READ|PROT_WRITE,MAP_SHARED,lcd,0);
	
	Frist = calloc(1,800*480*4);
	Frist = readFrist(lcd);
	
	showMain(fbmem,&vinfo,10,80);
	pid_t pid = getpid();
	pthread_t IconTid;
	pthread_t PlayTid;
	pthread_create(&IconTid,NULL,touchType,(void *)&pid);

	int new = leave;
	int old = leave;
	while(1)
	{
		old=new;
		new=touchkey(10,80); 
		if (old == new)
		{	continue;}
		if(old != new && old != leave)
		{
			display(key_on,fbmem,&vinfo,old*W_key+10,80);
			exitflag = false;
		}
		if(new == leave)
		{
			
			display(key_on,fbmem,&vinfo,old*W_key+10,80);
			exitflag = false;
			continue;
		}
		
		if(touchtype == soundPlus)
		{
			//pluscount++ ;
			sound= sound + 5*pluscount;
			printf("pluscount :%d\n",pluscount);
			pluscount = 0;
			if(sound >= 18)
				sound = 18;
			printf(" ++ sound %d\n",sound);
			touchtype = -1;
			//pluscount = 0;
		}
		if(touchtype == soundSub)
		{
			//subcount++;
			sound = sound - 5*subcount;
			printf("subscount :%d\n",subcount);
			subcount = 0;	
			if(sound <= -178)
			{
				sound = -178;
			}
			printf(" -- sound %d\n",sound);
			touchtype = -1;
			//subcount = 0;
		}
		
		display(key_off,fbmem,&vinfo,new*W_key+10,80);
		pthread_create(&PlayTid,NULL,play,(void *)&new); 

	}


}
